package com.neu.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.neu.R;
import com.neu.bean.RecBean;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class RecAdapter extends RecyclerView.Adapter<RecAdapter.VH> {
    List<RecBean> list;

    public RecAdapter(List<RecBean> list){
        this.list=list;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //解析xml文件，生成view对象
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rec,null);
        //返回VH
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        //获得一条数据
        RecBean r=list.get(position);
        holder.tvRec.setText(r.getContent());
        holder.imgRec.setImageResource(r.getImgId());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //用来保存列表的对象引用
    static class VH extends RecyclerView.ViewHolder{
        ImageView imgRec;
        TextView tvRec;

        public VH(@NonNull View itemView) {
            super(itemView);
            imgRec=itemView.findViewById(R.id.imgRec);
            tvRec=itemView.findViewById(R.id.tvRec);
        }
    }
}
